#include "taskTable.h"
#include "version.h"
#include "logger.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>

/**
 *  
 */
#define TASK_TABLE_NAME "t_task"
#define SQL_TASK_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " TASK_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, round INT NOT NULL, name TEXT NOT NULL, startTime TEXT NOT NULL, intervalTime INT NOT NULL, priority INT NOT NULL, isUsed INT NOT NULL)"
#define SQL_TASK_TABLE_INSERT(...) "INSERT INTO " TASK_TABLE_NAME "(id, round, name, startTime, intervalTime, priority, isUsed) VALUES (" #__VA_ARGS__ ")"
#define SQL_TASK_TABLE_REPLACE(...) "REPLACE INTO " TASK_TABLE_NAME "(id, round, name, startTime, intervalTime, priority, isUsed) VALUES (" #__VA_ARGS__ ")"
#define SQL_TASK_TABLE_DELETE(id) "DELETE FROM " TASK_TABLE_NAME " WHERE id = " #id
#define SQL_TASK_TABLE_SELECT(id) "SELECT * FROM " TASK_TABLE_NAME " WHERE id = " #id



/*===============================================================*/
/* 联动 */
int createTaskTable(){
    return db_create_table(SQL_TASK_TABLE_CREATE);
}

int insertTask(T_TASK* task){
    if(NULL == task){
        log_file_e(APPNAME, "insert task is NULL");
        return -1;
    }
    int ret = db_insert(SQL_TASK_TABLE_INSERT(task->id, task->round, task->isRound, task->name, task->desc));
    return ret;
}

int replaceTask(T_TASK* task){
    if(NULL == task){
        log_file_e(APPNAME, "replace task is NULL");
        return -1;
    }
    int ret = db_insert(SQL_TASK_TABLE_REPLACE(task->id, task->round, task->isRound, task->name, task->desc));
    return ret;
}

int deleteTask(T_TASK* task){
    if(NULL == task){
        log_file_e(APPNAME, "delete task is NULL");
        return -1;
    }
    return db_delete(SQL_TASK_TABLE_DELETE(task->id));
}
/**
 *  
 */
static int taskCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "taskCallback data is NULL");
        return -1;
    }
    T_TASK_LIST* taskList = (T_TASK_LIST*)data;
    if((0 == taskList->count) || (NULL == taskList->head)){
        taskList->head = taskList->tail = (T_TASK*)malloc(sizeof(T_TASK));
        taskList->head->next = NULL;
        taskList->head->prev = NULL;
    } else {
        taskList->tail->next = (T_TASK*)malloc(sizeof(T_TASK));
        taskList->tail->next->prev = taskList->tail;
        taskList->tail = taskList->tail->next;
    }
    taskList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            taskList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "round")){
            taskList->tail->round = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                taskList->tail->name[0] = '\0';
            } else {
                strcpy(taskList->tail->name, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "startTime")){
            if(NULL == argv[i]){
                taskList->tail->startTime[0] = '\0';
            } else {
                strcpy(taskList->tail->startTime, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "intervalTime")){
            taskList->tail->intervalTime = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "isUsed")){
            taskList->tail->isUsed = atoi(argv[i]);
        } 
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return taskList->count;
}

int selectTask(const char* sql, T_TASK_LIST* taskList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == taskList)){
        log_file_e(APPNAME, "select product sql is NULL");
        return -1;
    }
    return db_select(sql, taskCallback, taskList);

}
//
void dbTaskInit(){
    createTaskTable();
}
